package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McAccountInfoMapper;
import cc.rengu.igas.momp.common.dao.McInfoListMapper;
import cc.rengu.igas.momp.common.dao.impl.McAccountInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.McInfoListMapperImpl;
import cc.rengu.igas.momp.common.entity.TMAccountInfo;
import cc.rengu.igas.momp.common.entity.TMMcCashierCashbackInfo;
import cc.rengu.igas.momp.common.entity.TMMcUserInfo;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.request.CashierJoinCashierCashBackRequest;
import cc.rengu.igas.momp.facade.response.CashierJoinCashierCashBackResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 收银员参与收银员返现活动
 *
 * @author Jinan Liu
 * @date 2020/4/2 17:49
 * @description
 */
public class CashierJoinCashierCashBackService extends RadpService {
    private String globalInstId;
    private String userId;
    private String mcNo;
    private String acctNo;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            CashierJoinCashierCashBackRequest cashierJoinCashierCashBackRequest = new CashierJoinCashierCashBackRequest();
            ConvertUtil.convertOutput(cashierJoinCashierCashBackRequest);

            CashierJoinCashierCashBackResponse cashierJoinCashierCashBackResponse = new CashierJoinCashierCashBackResponse();
            cashierJoinCashierCashBackResponse.setHeader(cashierJoinCashierCashBackRequest.getHeader());
            BizResponse<CashierJoinCashierCashBackResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(cashierJoinCashierCashBackResponse);

            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalInstId = cashierJoinCashierCashBackRequest.getHeader().getInstId();
            userId = cashierJoinCashierCashBackRequest.getUserInfo().getUserId();
            mcNo = cashierJoinCashierCashBackRequest.getMcNo();
            /* 检查操作员机构标示 */
            if (StringUtil.isNullorEmpty(globalInstId)) {
                rglog.error("法人机构号<{}>为空!", globalInstId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }

            // 服务调用
            BizResponse<CashierJoinCashierCashBackResponse> bizResponseNew = cashierJoinCashierBack(xmlTreeUtil, cashierJoinCashierCashBackRequest, cashierJoinCashierCashBackResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 收银员参与收银员返现活动
     *
     * @param xmlTreeUtil                        内部XML树
     * @param cashierJoinCashierCashBackRequest  收银员参与收银员返现活动接口请求报文
     * @param cashierJoinCashierCashBackResponse 收银员参与收银员返现活动接口应答报文
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.CashierJoinCashierCashBackResponse>收银员参与收银员返现活动接口应答报文
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 14:50
     */

    private BizResponse<CashierJoinCashierCashBackResponse> cashierJoinCashierBack(XmlTreeUtil xmlTreeUtil, CashierJoinCashierCashBackRequest cashierJoinCashierCashBackRequest, CashierJoinCashierCashBackResponse cashierJoinCashierCashBackResponse) throws Exception {

        BizResponse<CashierJoinCashierCashBackResponse> bizResponse = new BizResponse<>();
        McAccountInfoMapper mcAccountInfoMapper = new McAccountInfoMapperImpl();
        /**判断是否存在收银员信息*/
        TMAccountInfo tmAccountInfo = mcAccountInfoMapper.selectMcAccountInfoByInstIdUserId(globalInstId, userId);
        if (null == tmAccountInfo) {
            rglog.error("根据法人机构号<{}>用户标识<{}>查询收银员信息表信息不存在!", globalInstId, userId);
            throw new BizException(RespCodeEnum.ACCOUNT_INFO_NULL.getRespCode(), RespCodeEnum.ACCOUNT_INFO_NULL.getRespDesc());
        }
        if (null != tmAccountInfo.getUserId() && !tmAccountInfo.getUserId().isEmpty()) {
            userId = tmAccountInfo.getUserId();
        }
        /**判断是否存在营销活动用户信息表*/
        TMMcUserInfo tmMcUserInfo = mcAccountInfoMapper.selcetMcMchntInfoByInstIdMcNoAcctNo(globalInstId, mcNo, userId);
        if (null != tmMcUserInfo) {
            rglog.error("根据法人机构号<{}>营销活动编号<{}>账号<{}>查询营销活动用户信息表信息已存在!", globalInstId, mcNo, userId);
            throw new BizException(RespCodeEnum.MC_USER_EXISTENCE.getRespCode(), RespCodeEnum.MC_USER_EXISTENCE.getRespDesc());
        }
        /**判断是否存在收银员返现活动*/
        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        TMMcCashierCashbackInfo tmMcCashierCashbackInfo = mcInfoListMapper.selectCashierCashbackByInstIdMcNo(globalInstId, mcNo);
        if (null == tmMcCashierCashbackInfo) {
            rglog.error("根据法人机构号<{}>营销活动编号<{}>查询收银员返现类活动信息失败!", globalInstId, mcNo);
            throw new BizException(RespCodeEnum.GET_CASHIER_CASHBACK_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_CASHIER_CASHBACK_MC_DETAIL_ERROR.getRespDesc());
        }
        tmMcUserInfo = convertMcUserInfo(tmAccountInfo, cashierJoinCashierCashBackRequest);
        int ReturnCode = mcAccountInfoMapper.insertMcUserInfo(tmMcUserInfo);
        if (Database.DBS_SUCCESS != ReturnCode) {
            /* 异常结束数据库事务 */
            rglog.error("新增营销活动用户信息失败, 用户标识<{}>, 账号<{}>，营销活动编号<{}>", userId, acctNo, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        cashierJoinCashierCashBackResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        cashierJoinCashierCashBackResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(cashierJoinCashierCashBackResponse);

        return bizResponse;
    }

    /**
     * @param tmAccountInfo                     收银员信息实体类
     * @param cashierJoinCashierCashBackRequest 收银员参与收银员返现活动接口请求报文
     * @return
     * @throws BizException
     */
    private TMMcUserInfo convertMcUserInfo(TMAccountInfo tmAccountInfo, CashierJoinCashierCashBackRequest cashierJoinCashierCashBackRequest) throws BizException {
        TMMcUserInfo tmMcUserInfo = new TMMcUserInfo();
        /** 机构号*/
        tmMcUserInfo.setInstId(globalInstId);
        /** 营销活动编号*/
        tmMcUserInfo.setMcNo(mcNo);
        /** 用户账号*/
        tmMcUserInfo.setAcctNo(userId);
        /**账户类型 9-收银员Id*/
        tmMcUserInfo.setAcctType("9");
        /**账户标志 1-正式账户*/
        tmMcUserInfo.setAcctFlag("1");
        /**会员Id 0-收银员*/
        tmMcUserInfo.setMemberId("0");
        /**创建人*/
        tmMcUserInfo.setOprId(userId);
        /**创建时间*/
        tmMcUserInfo.setCreateTime(new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date()));

        return tmMcUserInfo;
    }
}
